package com.ohos.apollo_hos.slice;

import com.apollographql.apollo.rxhos.simple.base.BaseModel;
import com.ohos.apollo_hos.ResourceTable;
import com.ohos.apollo_hos.adapter.NewsListAdapter;
import com.ohos.apollo_hos.bean.MainBean;
import com.ohos.apollo_hos.loading.LoadingUtil;
import com.ohos.apollo_hos.util.NetworkUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;
import ohos.net.NetCapabilities;
import ohos.net.NetHandle;
import ohos.net.NetManager;
import ohos.net.NetStatusCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * 列表页面
 */
public class MainAbilitySlice extends AbilitySlice {
    private ListContainer listContent;
    private NewsListAdapter newsListAdapter;
    private List<MainBean> newsInfoList;
    private String a1 = "CCAFS SLC ";
    private String a2 = "VAFB SLC ";
    private String a3 = "KSC LC ";
    private BaseModel baseModel;
    private int first = 0;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        baseModel = new BaseModel();
        baseModel.init();
        LoadingUtil.hideDiading(MainAbilitySlice.this, ResourceTable.Layout_ability_main);
        // 获取网络管理对象

    }

    public void initLayout() {
        listContent = (ListContainer) findComponentById(ResourceTable.Id_list_content);
        newsInfoList = new ArrayList<>();
        String b = "";
        for (int a = 0; a < 10; a++) {
            int c = 11 - a;
            if (a % 2 == 0) {
                b = a1 + c;
            } else if (a % 3 == 0) {
                b = a2 + c;
            } else {
                b = a3 + c;
            }
            newsInfoList.add(new MainBean(a, "Starlink-" + (a + 1) + " (v1.0)", b));

        }
        newsListAdapter = new NewsListAdapter(newsInfoList, this);
        listContent.setItemProvider(newsListAdapter);
        listContent.setItemClickedListener((listContainer, component, i, l) -> {
            Intent intent1 = new Intent();
            intent1.setParam("item", (MainBean) newsListAdapter.getItem(i));
            present(new DetailAbilitySlice(), intent1);
        });
    }

    @Override
    public void onActive() {
        super.onActive();
        if (NetworkUtil.isNetworkConnected(MainAbilitySlice.this)) {
            // 网络连接成功，并且可以上网
            if (NetworkUtil.isWifiConnectedInternet(MainAbilitySlice.this)) {
                initLayout();
            } else if (NetworkUtil.isMobileConnectedInternet(MainAbilitySlice.this)) {
                initLayout();
            }
        }
    }

}
